//
//  NSBundle+MCLanguage.m
//  LanKa
//
//  Created by MINI on 2023/9/5.
//  Copyright © 2023 MC. All rights reserved.
//


#import "NSBundle+MCLanguage.h"
#import <objc/runtime.h>
static const char _bundle = 0;

@interface BundleEx : NSBundle

@end

@implementation BundleEx

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {

    NSBundle *bundle = objc_getAssociatedObject(self, &_bundle);
    return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super localizedStringForKey:key value:value table:tableName];
}

@end

@implementation NSBundle (MCLanguage)
+ (void)setLanguage:(NSString *)language {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        object_setClass([NSBundle mainBundle], [BundleEx class]);
    });

    objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}



//获取系统的语言（这个和用户选择的没关系，用户选择的需要你自己保存记忆）
//（这个功能看业务逻辑，或许用不到
//NSArray *appleLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
//if (appleLanguages.count > 0) {
//    NSString *currentLanguage = appleLanguages[0];
//    // 在这里判断 currentLanguage 是哪国的语言，来处理你的逻辑
//    }


@end
